package com.qf.eduadmin.service.impl;

import com.qf.eduadmin.mapper.TbUserDao;
import com.qf.eduadmin.mapper.jpa.TbUserRepository;
import com.qf.eduadmin.pojo.req.TbUserReq;
import com.qf.eduadmin.pojo.req.TbUserRoleReq;
import com.qf.eduadmin.pojo.resp.ResponseData;
import com.qf.eduadmin.pojo.resp.TbUserRoleResp;
import com.qf.eduadmin.pojo.resp.TreeResp;
import com.qf.eduadmin.pojo.vo.TbRole;
import com.qf.eduadmin.pojo.vo.TbUser;
import com.qf.eduadmin.pojo.vo.TbUserRole;
import com.qf.eduadmin.service.TbUserService;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Mr.Liu
 * @date 2021/12/27 19:05
 */
@Service
public class TbUserServiceImpl implements TbUserService {
    @Resource
    private TbUserRepository tbUserRepository;

    @Resource
    private TbUserDao tbUserDao;

    @Override
    public ResponseData login(TbUserReq tbUserReq, HttpServletRequest request) {
        String captcha = (String) request.getSession().getAttribute("captcha");
        if(!tbUserReq.getCaptcha().equalsIgnoreCase(captcha)){
            return new ResponseData(1,"验证码错误",null,null);
        }
    /*    TbUser tbUser = tbUserRepository.findByUserName(tbUserReq.getUserName());
        if (tbUser==null){
            return new ResponseData(1,"没有此管理员",null,null);
        }
        if (!tbUser.getPassWord().equals(tbUserReq.getPassWord())){
            return new ResponseData(1,"密码错误",null,null);
        }
        request.getSession().setAttribute("tbuser",tbUser);*/

        //使用shiro进行登录验证
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(tbUserReq.getUserName(),tbUserReq.getPassWord());
        try{
            subject.login(usernamePasswordToken);
        }catch (UnknownAccountException unknownAccountException){
            return new ResponseData(1,"没有此用户",null,null);
        }catch (IncorrectCredentialsException incorrectCredentialsException){
            return new ResponseData(1,"密码错误",null,null);
        }
        return new ResponseData(0,"登录成功",null,null);
    }

    /**
     * 查询所有用户
     * @param page
     * @param limit
     * @return
     */
    @Override
    public ResponseData findAll(Integer page, Integer limit) {
        //pagehelp 页码从1开始，jpa分页页码从0 开始，所以需要page-1
        PageRequest of = PageRequest.of(page - 1, limit);
        Page<TbUser> users = tbUserRepository.findAll(of);
        return new ResponseData(0,"查询成功",users.getContent(),users.getTotalElements());
    }

    /**
     * 通过id删除用户
     * @param id
     * @return
     */
    @Override
    public ResponseData deleteUserById(Integer id) {
        tbUserRepository.deleteById(id);
        return new ResponseData(0,"删除成功",null,null);
    }

    /**
     * 新增用户
     * @param tbUser
     * @return
     */
    @Override
    public ResponseData insertUser(TbUser tbUser) {
        int insert = tbUserDao.insert(tbUser);
        if(insert>0){
            return new ResponseData(0,"添加成功",null,null);
        }
        return new ResponseData(1,"添加失败",null,null);
    }

    /**
     * 更新用户角色
     * @param tbUserRoleReq
     * @return
     */
    @Override
    public ResponseData updateRole(TbUserRoleReq tbUserRoleReq) {
        //1.首先删除中间表用户与角色的关系
        Integer integer = tbUserDao.deleteRoleByUserid(tbUserRoleReq.getUserid());
        List<TreeResp> roleReqList = tbUserRoleReq.getPerlist();
        //2.重新添加用户与角色的关系
        if(roleReqList.size()>0){
            List<TbUserRole> list = new ArrayList<>();
            for(TreeResp treeResp : roleReqList){
                //添加中间表
                TbUserRole tbUserRole = new TbUserRole();
                //添加用户id
                tbUserRole.setUserid(tbUserRoleReq.getUserid());
                //添加角色id
                tbUserRole.setRoleid(treeResp.getId());
                list.add(tbUserRole);
            }
            Integer integer1 = tbUserDao.insertAll(list);
        }
        return new ResponseData(0,"新增成功",null,null);
    }


}
